// -*-c#-*-
// License: New BSD License : http://www.opensource.org/licenses/bsd-license.php
//
// Copyright (c) 2008, Brothers In Arms/Burl Nyswonger
//
// See: BiaVote.uc for full License
// 
//@L


class BiaVoteDataStore extends UIDataStore
    Config(BiaVote);
    
`include( BiaVote/Classes/BiaStdInc.uci )

// ----------------------------------------------------------------------------

var array<string> aMessage;

// these need to be parallel
var array<string> aLabels;
var array<string> aTags;

event GetSupportedScriptFields( out array<UIDataProviderField> out_Fld )
{
    local UIDataProviderField Fld;
        
    Fld.FieldTag  ='WelcomeMessage';
    Fld.FieldType = DATATYPE_Collection;
    out_Fld[out_Fld.Length] = Fld;
    
    Fld.FieldTag = 'VoteSceneLabels';
    Fld.FieldType = DATATYPE_Collection;
    out_Fld[out_Fld.Length] = Fld;

}

event bool GetFieldValue( string FieldName, out UIProviderScriptFieldValue FieldValue, optional int ArrayIndex=INDEX_NONE )
{
    local bool bResult;
    local int i;

    switch ( FieldName )
    {
    case "lbl_biavote":
        FieldValue.StringValue = aLabels[0];
        FieldValue.PropertyType = DATATYPE_Property;
        bResult = true;
        break;
    case "Message":
        if(ArrayIndex >= 0 && ArrayIndex < aMessage.Length )
        {
            bResult = true;
            FieldValue.StringValue = aMessage[ArrayIndex];
            `BiaDbg("Retrieved Message["$ArrayIndex$"] = " $ FieldValue.StringValue );
        }
        else
        {
            `BiaDbg("Failed to retrieve Message["$ArrayIndex$"]");
        }
        break;
    case "Labels":
        if( ArrayIndex >= 0 && ArrayIndex < aLabels.Length )
        {
            bResult = true;
            FieldValue.StringValue = aLabels[ArrayIndex];
        }
        break;
    default:
        // hehe - this is an awful hack...
        `BiaDbg("Searching Labels array for '"$ FieldName $"'...");
        i = aTags.Find(FieldName);
        if( i > -1 )
        {
            FieldValue.StringValue = aLabels[i];
            FieldValue.PropertyType = DATATYPE_Property;
            `BiaDbg("Found! - returning: '"$ FieldValue.StringValue $"'");
            bResult = true;
        }
    }
    
`if(`isdefined(DEBUG))
    if( !bResult )
    {
        `BiaDbg("Did not find the data");
    }
`endif
    
    return(bResult);
}

event bool SetFieldValue( string FieldName, const out UIProviderScriptFieldValue FieldValue, optional int ArrayIndex=INDEX_NONE )
{
    local bool bResult;
    local string t, v;
    local int i;
    
    switch ( FieldName )
    {
    case "Message":
        if( ArrayIndex >= 0 && ArrayIndex <= aMessage.Length )
        {
            bResult = true;
            aMessage[ArrayIndex] = FieldValue.StringValue;
            `BiaDbg("Added Message[" $ ArrayIndex $ "] --> " $ aMessage[ArrayIndex]);
        }
        else
        {
            `BiaDbg("Failed to add to Message[], Length=" $ aMessage.Length $ ", ArrayIndex=" $ ArrayIndex);
        }
        break;
    case "Labels":
        if( ArrayIndex >= 0 && ArrayIndex <= aLabels.Length )
        {
            i = Instr(FieldValue.StringValue, ":");
            if( i > 0 && (i+1) < Len(FieldValue.StringValue))
            {
                t = Left(FieldValue.StringValue, i);
                v = Mid(FieldValue.StringValue, i+1);

                `BiaAssert( aLabels.Length == aTags.Length );
                
                aLabels[ArrayIndex] = v;
                aTags[ArrayIndex] = t;
                
                `BiaDbg("Tag["$ aTags[ArrayIndex] $"] - Value["$ aLabels[ArrayIndex] $"]");
                
                bResult = true;
            }
        }
        break;
    }
    if( bResult )
    {
//TODO: when support for mapping a different vote-key happens, do SaveConfig()
//        SaveConfig();
    }
    return(bResult);
}
        
// ----------------------------------------------------------------------------

defaultproperties
{
    aLabels(0)="[BIA]Vote"
    aTags(0)="BiaVoteTitle"
    aLabels(1)="[BIA]Welcome"
    aTags(1)="BiaWelcomeTitle"
    
    aMessage(0)="Welcome!"
    
    Tag="BiaVoteDataStore"
    WriteAccessType=ACCESS_WriteAll

}
